const { get } = require('../../utils/request')

Page({
  data: {
    breakfastRecipes: [],
    lunchRecipes: [],
    dinnerRecipes: []
  },

  onLoad() {
    this.loadRecipes()
  },

  async loadRecipes() {
    try {
      wx.showLoading({
        title: '加载中...'
      })

      // 加载早餐食谱
      const breakfastRes = await get('/recipes', {
        category: '早餐',
        limit: 6
      })
      
      // 加载午餐食谱
      const lunchRes = await get('/recipes', {
        category: '午餐',
        limit: 6
      })
      
      // 加载晚餐食谱
      const dinnerRes = await get('/recipes', {
        category: '晚餐',
        limit: 6
      })

      this.setData({
        breakfastRecipes: breakfastRes.recipes,
        lunchRecipes: lunchRes.recipes,
        dinnerRecipes: dinnerRes.recipes
      })

    } catch (error) {
      console.error('加载食谱失败:', error)
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      })
    } finally {
      wx.hideLoading()
    }
  },

  viewRecipeDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/recipe/detail?id=${id}`
    })
  },

  viewMore(e) {
    const type = e.currentTarget.dataset.type
    wx.navigateTo({
      url: `/pages/recipe/category?type=${type}`
    })
  },

  onPullDownRefresh() {
    this.loadRecipes().then(() => {
      wx.stopPullDownRefresh()
    })
  }
}) 